package com.example.hack1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        final EditText et = (EditText) findViewById(R.id.textView1);
    
        Button b1 = (Button) findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				HttpClient mClient= new DefaultHttpClient();
				
				//get lac
				final TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
				if (telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) 
				{
				final GsmCellLocation location = (GsmCellLocation) telephony.getCellLocation();
												
				HttpGet httppost = new HttpGet("http://myapp.zymichost.com/hack2/index.php?LAC="+location.getLac());
				
				try {
				
				HttpResponse res = mClient.execute(httppost);
				HttpEntity entity = res.getEntity();

				
				 if (entity != null) {
					 
					 Toast toast =Toast.makeText(getApplicationContext(), "vivek", Toast.LENGTH_LONG);
					 toast.show();

				// A Simple JSON Response Read
				InputStream instream = entity.getContent();
				String result= convertStreamToString(instream);
				// now you have the string representation of the HTML request
				//et.setText();
				Toast toast1 =Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG);
				 toast1.show();
				instream.close();
				}

				} catch (Exception e) {
            // TODO Auto-generated catch block
					e.printStackTrace();
						}
				}
				
			}
			
			 private String convertStreamToString(InputStream is) {
    /*
     * To convert the InputStream to String we use the BufferedReader.readLine()
     * method. We iterate until the BufferedReader return null which means
     * there's no more data to read. Each line will appended to a StringBuilder
     * and returned as String.
     */
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

		});
    
    }

    
}
